package com.fuluwa.interfaces.product.facade;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fuluwa.domain.category.model.Category;
import com.fuluwa.domain.category.repository.CategoryRepository;
import com.fuluwa.domain.file.model.FileInfo;
import com.fuluwa.domain.product.model.Product;
import com.fuluwa.domain.product.repository.ProductRepository;
import com.fuluwa.domain.security.model.User;
import com.fuluwa.domain.security.repository.UserRepository;
import com.fuluwa.domain.usercenter.model.UserProfile;
import com.fuluwa.domain.usercenter.respository.UserProfileRepository;
import com.fuluwa.infra.context.UserUtils;
import com.fuluwa.infra.exception.PersistenceException;
import com.fuluwa.infra.jms.MessageProducer;
import com.google.common.collect.Maps;

@Service
public class ProductServiceFacadeImpl implements ProductServiceFacade {
	@Autowired
	CategoryRepository categoryRepository;
	@Autowired
	ProductRepository productRepository;
	@Autowired
	UserRepository userRepository;
	@Autowired
	UserProfileRepository userProfileRepository;
	@Autowired
	MessageProducer messageProducer;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fuluwa.interfaces.product.facade.ProductServiceFacade#findByLevel
	 * (int)
	 */
	@Override
	public Map<String, String> findByLevel(int level) {
		Map<String, String> ret = Maps.newHashMap();
		return ret;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fuluwa.interfaces.product.facade.ProductServiceFacade#findByParnet
	 * (com.fuluwa.domain.product.model.Category)
	 */
	@Override
	public Map<String, String> findByParnet(Category parnet) {
		Map<String, String> ret = Maps.newHashMap();
		return ret;
	}

	@Override
	@Transactional
	public void saveProduct(ProductDTO productDTO) throws PersistenceException {
		FileInfo f = new FileInfo(productDTO.getFileId());
		User user = userRepository.findByLoginName(UserUtils.getCurrentUserName());
		UserProfile userProfile = userProfileRepository.findByUser(user);
		// Category cate=new Category(productDTO.getCategoryId());
		Category cate = new Category();
		Product product = new Product(productDTO.getProductName(), cate, productDTO.getPrice(),
				productDTO.getKeywords(), productDTO.getIntroduction(), f, userProfile);
		product.save();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", product.getId());

		messageProducer.sendFileUploadQueue(map);
	}

}
